{
 "cells": [
  {
   "cell_type": "raw",
   "id": "473081cc",
   "metadata": {},
   "source": [
    "---\n",
    "sidebar_position: 1\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "16ee4216",
   "metadata": {},
   "source": [
    "# Streaming\n",
    "\n",
    "Streaming is an important UX consideration for LLM apps, and agents are no exception. Streaming with agents is made more complicated by the fact that it's not just tokens that you will want to stream, but you may also want to stream back the intermediate steps an agent takes.\n",
    "\n",
    "Let's take a look at how to do this."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "def159c3",
   "metadata": {},
   "source": [
    "## Set up the agent\n",
    "\n",
    "Let's set up a simple agent for demonstration purposes. For our tool, we will use [Tavily](/docs/integrations/tools/tavily_search). Make sure that you've exported an API key with \n",
    "\n",
    "```bash\n",
    "export TAVILY_API_KEY=\"...\"\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "670078c4",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_community.tools.tavily_search import TavilySearchResults\n",
    "\n",
    "search = TavilySearchResults()\n",
    "tools = [search]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5e04164b",
   "metadata": {},
   "source": [
    "We will use a prompt from the hub - you can inspect the prompt more at [https://smith.langchain.com/hub/hwchase17/openai-functions-agent](https://smith.langchain.com/hub/hwchase17/openai-functions-agent)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d8c5d907",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain import hub\n",
    "from langchain.agents import AgentExecutor, create_openai_functions_agent\n",
    "from langchain.chat_models import ChatOpenAI\n",
    "\n",
    "# Get the prompt to use - you can modify this!\n",
    "# If you want to see the prompt in full, you can at: https://smith.langchain.com/hub/hwchase17/openai-functions-agent\n",
    "prompt = hub.pull(\"hwchase17/openai-functions-agent\")\n",
    "\n",
    "llm = ChatOpenAI(model=\"gpt-3.5-turbo\", temperature=0)\n",
    "\n",
    "agent = create_openai_functions_agent(llm, tools, prompt)\n",
    "agent_executor = AgentExecutor(agent=agent, tools=tools)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cba9a9eb",
   "metadata": {},
   "source": [
    "## Stream intermediate steps\n",
    "\n",
    "Let's look at how to stream intermediate steps. We can do this easily by just using the `.stream` method on the AgentExecutor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b6bd9bf2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'actions': [AgentActionMessageLog(tool='tavily_search_results_json', tool_input={'query': 'weather in San Francisco'}, log=\"\\nInvoking: `tavily_search_results_json` with `{'query': 'weather in San Francisco'}`\\n\\n\\n\", message_log=[AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\\n  \"query\": \"weather in San Francisco\"\\n}', 'name': 'tavily_search_results_json'}})])], 'messages': [AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\\n  \"query\": \"weather in San Francisco\"\\n}', 'name': 'tavily_search_results_json'}})]}\n",
      "------\n",
      "{'steps': [AgentStep(action=AgentActionMessageLog(tool='tavily_search_results_json', tool_input={'query': 'weather in San Francisco'}, log=\"\\nInvoking: `tavily_search_results_json` with `{'query': 'weather in San Francisco'}`\\n\\n\\n\", message_log=[AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\\n  \"query\": \"weather in San Francisco\"\\n}', 'name': 'tavily_search_results_json'}})]), observation=[{'url': 'https://weather.com/weather/tenday/l/San Francisco CA USCA0987:1:US', 'content': 'recents Specialty Forecasts 10 Day Weather-San Francisco, CA Today Mon 18 | Day  Fri 22 Fri 22 | Day Foggy early, then partly cloudy later in the day. High around 60F. Winds W at 10 to 15 mph.  Considerable cloudiness with occasional rain showers. High 59F. Winds SSE at 5 to 10 mph. Chance of rain 50%.  Thu 28 | Night Cloudy with showers. Low 46F. Winds S at 5 to 10 mph. Chance of rain 40%. Fri 29 Fri 29 | Day10 Day Weather-San Francisco, CA As of 5:52 pm PST alertLevel3 Coastal Flood Advisory+1 More Tonight Mostly Cloudy Night --/44° Rain 7% Arrow Up Sun 24| Night 44° Mostly Cloudy Night Rain 7%...'}])], 'messages': [FunctionMessage(content='[{\"url\": \"https://weather.com/weather/tenday/l/San Francisco CA USCA0987:1:US\", \"content\": \"recents Specialty Forecasts 10 Day Weather-San Francisco, CA Today Mon 18 | Day  Fri 22 Fri 22 | Day Foggy early, then partly cloudy later in the day. High around 60F. Winds W at 10 to 15 mph.  Considerable cloudiness with occasional rain showers. High 59F. Winds SSE at 5 to 10 mph. Chance of rain 50%.  Thu 28 | Night Cloudy with showers. Low 46F. Winds S at 5 to 10 mph. Chance of rain 40%. Fri 29 Fri 29 | Day10 Day Weather-San Francisco, CA As of 5:52 pm PST alertLevel3 Coastal Flood Advisory+1 More Tonight Mostly Cloudy Night --/44° Rain 7% Arrow Up Sun 24| Night 44° Mostly Cloudy Night Rain 7%...\"}]', name='tavily_search_results_json')]}\n",
      "------\n",
      "{'actions': [AgentActionMessageLog(tool='tavily_search_results_json', tool_input={'query': 'weather in Los Angeles'}, log=\"\\nInvoking: `tavily_search_results_json` with `{'query': 'weather in Los Angeles'}`\\n\\n\\n\", message_log=[AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\\n  \"query\": \"weather in Los Angeles\"\\n}', 'name': 'tavily_search_results_json'}})])], 'messages': [AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\\n  \"query\": \"weather in Los Angeles\"\\n}', 'name': 'tavily_search_results_json'}})]}\n",
      "------\n",
      "{'steps': [AgentStep(action=AgentActionMessageLog(tool='tavily_search_results_json', tool_input={'query': 'weather in Los Angeles'}, log=\"\\nInvoking: `tavily_search_results_json` with `{'query': 'weather in Los Angeles'}`\\n\\n\\n\", message_log=[AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\\n  \"query\": \"weather in Los Angeles\"\\n}', 'name': 'tavily_search_results_json'}})]), observation=[{'url': 'https://weather.com/weather/tenday/l/Los+Angeles+CA?canonicalCityId=84c64154109916077c8d3c2352410aaae5f6eeff682000e3a7470e38976128c2', 'content': 'recents Specialty Forecasts 10 Day Weather-Los Angeles, CA Air Quality Alert Today Tue 26 | Day  Considerable cloudiness with occasional rain showers. High 64F. Winds light and variable. Chance of rain 50%.  Considerable cloudiness with occasional rain showers. High 62F. Winds light and variable. Chance of rain 60%.  Wed 03 Wed 03 | Day Overcast with showers at times. High 66F. Winds light and variable. Chance of rain 40%.Today 66°/ 50° 6% Sun 24 | Day 66° 6% WSW 4 mph Partly cloudy skies. High 66F. Winds light and variable. Humidity 65% UV Index 3 of 11 Sunrise 6:56 am Sunset 4:49 pm Sun 24 | Night 50° 10% N 1 mph...'}, {'url': 'https://weather.com/weather/tenday/l/Los+Angeles+CA?canonicalCityId=d4a04df2f5cd7d6ef329c49238253e994619763fd5f77a424ca3f1af9957e717', 'content': 'recents Specialty Forecasts 10 Day Weather-Los Angeles, CA Air Quality Alert Today Tue 26 | Day  Rain showers early with some sunshine later in the day. High 61F. Winds SSE at 5 to 10 mph. Chance of rain 60%.  Thu 04 Thu 04 | Day Overcast with rain showers at times. High 63F. Winds S at 5 to 10 mph. Chance of rain 50%.  Wed 03 Wed 03 | Day Cloudy with occasional showers. High 63F. Winds SE at 5 to 10 mph. Chance of rain 50%.10 Day Weather - Los Angeles, CA As of 12:06 am PST Tonight --/ 49° 5% Sat 23 | Night 49° 5% NNW 2 mph Partly cloudy this evening, then becoming foggy and damp after midnight. Low 49F. Winds...'}, {'url': 'https://weather.com/weather/hourbyhour/l/Los+Angeles+CA?canonicalCityId=84c64154109916077c8d3c2352410aaae5f6eeff682000e3a7470e38976128c2', 'content': 'recents Specialty Forecasts Hourly Weather-Los Angeles, CA Air Quality Alert Tuesday, December 26 10 am Partly Cloudy  Cold & Flu Forecast Flu risk is low in your area  3 pm Partly Cloudy 4 pm Mostly Cloudy 5 pm Mostly Cloudy 6 pm Mostly Cloudy 7 pm Cloudy 8 pm Cloudy 9 pm Mostly Cloudy  10 am Cloudy 11 am Mostly Cloudy 12 pm Cloudy 1 pm Mostly Cloudy 2 pm Cloudy 3 pm Cloudy 4 pm Cloudy 5 pm Cloudy 6 pmHourly Weather Forecast for Los Angeles, CA - The Weather Channel | Weather.com - Los Angeles, CA As of 11:12 am PST Saturday, December 23 12 pm 64° 4% Mostly Cloudy Feels Like 64°...'}])], 'messages': [FunctionMessage(content='[{\"url\": \"https://weather.com/weather/tenday/l/Los+Angeles+CA?canonicalCityId=84c64154109916077c8d3c2352410aaae5f6eeff682000e3a7470e38976128c2\", \"content\": \"recents Specialty Forecasts 10 Day Weather-Los Angeles, CA Air Quality Alert Today Tue 26 | Day  Considerable cloudiness with occasional rain showers. High 64F. Winds light and variable. Chance of rain 50%.  Considerable cloudiness with occasional rain showers. High 62F. Winds light and variable. Chance of rain 60%.  Wed 03 Wed 03 | Day Overcast with showers at times. High 66F. Winds light and variable. Chance of rain 40%.Today 66°/ 50° 6% Sun 24 | Day 66° 6% WSW 4 mph Partly cloudy skies. High 66F. Winds light and variable. Humidity 65% UV Index 3 of 11 Sunrise 6:56 am Sunset 4:49 pm Sun 24 | Night 50° 10% N 1 mph...\"}, {\"url\": \"https://weather.com/weather/tenday/l/Los+Angeles+CA?canonicalCityId=d4a04df2f5cd7d6ef329c49238253e994619763fd5f77a424ca3f1af9957e717\", \"content\": \"recents Specialty Forecasts 10 Day Weather-Los Angeles, CA Air Quality Alert Today Tue 26 | Day  Rain showers early with some sunshine later in the day. High 61F. Winds SSE at 5 to 10 mph. Chance of rain 60%.  Thu 04 Thu 04 | Day Overcast with rain showers at times. High 63F. Winds S at 5 to 10 mph. Chance of rain 50%.  Wed 03 Wed 03 | Day Cloudy with occasional showers. High 63F. Winds SE at 5 to 10 mph. Chance of rain 50%.10 Day Weather - Los Angeles, CA As of 12:06 am PST Tonight --/ 49° 5% Sat 23 | Night 49° 5% NNW 2 mph Partly cloudy this evening, then becoming foggy and damp after midnight. Low 49F. Winds...\"}, {\"url\": \"https://weather.com/weather/hourbyhour/l/Los+Angeles+CA?canonicalCityId=84c64154109916077c8d3c2352410aaae5f6eeff682000e3a7470e38976128c2\", \"content\": \"recents Specialty Forecasts Hourly Weather-Los Angeles, CA Air Quality Alert Tuesday, December 26 10 am Partly Cloudy  Cold & Flu Forecast Flu risk is low in your area  3 pm Partly Cloudy 4 pm Mostly Cloudy 5 pm Mostly Cloudy 6 pm Mostly Cloudy 7 pm Cloudy 8 pm Cloudy 9 pm Mostly Cloudy  10 am Cloudy 11 am Mostly Cloudy 12 pm Cloudy 1 pm Mostly Cloudy 2 pm Cloudy 3 pm Cloudy 4 pm Cloudy 5 pm Cloudy 6 pmHourly Weather Forecast for Los Angeles, CA - The Weather Channel | Weather.com - Los Angeles, CA As of 11:12 am PST Saturday, December 23 12 pm 64° 4% Mostly Cloudy Feels Like 64°...\"}]', name='tavily_search_results_json')]}\n",
      "------\n",
      "{'output': \"The weather in San Francisco is currently foggy early, then partly cloudy later in the day with a high around 60°F. There is a chance of rain showers with a high of 59°F tomorrow. The temperature will drop to a low of 46°F on Thursday night with cloudy conditions and showers.\\n\\nIn Los Angeles, there is considerable cloudiness with occasional rain showers today. The high temperature is expected to be 64°F with light and variable winds. Tomorrow, there will be considerable cloudiness with occasional rain showers and a high of 62°F. The weather will be overcast with showers at times on Wednesday with a high of 66°F.\\n\\nPlease note that weather conditions can change rapidly, so it's always a good idea to check a reliable weather source for the most up-to-date information.\", 'messages': [AIMessage(content=\"The weather in San Francisco is currently foggy early, then partly cloudy later in the day with a high around 60°F. There is a chance of rain showers with a high of 59°F tomorrow. The temperature will drop to a low of 46°F on Thursday night with cloudy conditions and showers.\\n\\nIn Los Angeles, there is considerable cloudiness with occasional rain showers today. The high temperature is expected to be 64°F with light and variable winds. Tomorrow, there will be considerable cloudiness with occasional rain showers and a high of 62°F. The weather will be overcast with showers at times on Wednesday with a high of 66°F.\\n\\nPlease note that weather conditions can change rapidly, so it's always a good idea to check a reliable weather source for the most up-to-date information.\")]}\n",
      "------\n"
     ]
    }
   ],
   "source": [
    "for chunk in agent_executor.stream({\"input\": \"what is the weather in SF and then LA\"}):\n",
    "    print(chunk)\n",
    "    print(\"------\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "433c78f0",
   "metadata": {},
   "source": [
    "You can see that we get back a bunch of different information. There are two ways to work with this information:\n",
    "\n",
    "1. By using the AgentAction/observation/AgentFinish object\n",
    "2. By using the `messages` object\n",
    "\n",
    "You may prefer to use the `messages` object if you are working with a chatbot - because these are chat messages and can be rendered directly. If you don't care about that, the AgentAction/observation/AgentFinish is probably the easier one to inspect."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "edd291a7",
   "metadata": {},
   "source": [
    "### Using AgentAction/observation/AgentFinish\n",
    "\n",
    "You can access these raw objects as part of the streamed payload. This gives you more low level information, but can be harder to parse."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "603bff1d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Calling Tool ```tavily_search_results_json``` with input ```{'query': 'weather in San Francisco'}```\n",
      "------\n",
      "Got result: ```[{'url': 'https://weather.com/weather/tenday/l/San Francisco CA USCA0987:1:US', 'content': 'recents Specialty Forecasts 10 Day Weather-San Francisco, CA Today Mon 18 | Day  Fri 22 Fri 22 | Day Foggy early, then partly cloudy later in the day. High around 60F. Winds W at 10 to 15 mph.  Considerable cloudiness with occasional rain showers. High 59F. Winds SSE at 5 to 10 mph. Chance of rain 50%.  Thu 28 | Night Cloudy with showers. Low 46F. Winds S at 5 to 10 mph. Chance of rain 40%. Fri 29 Fri 29 | Day10 Day Weather-San Francisco, CA As of 5:52 pm PST alertLevel3 Coastal Flood Advisory+1 More Tonight Mostly Cloudy Night --/44° Rain 7% Arrow Up Sun 24| Night 44° Mostly Cloudy Night Rain 7%...'}]```\n",
      "------\n",
      "Calling Tool ```tavily_search_results_json``` with input ```{'query': 'weather in Los Angeles'}```\n",
      "------\n",
      "Got result: ```[{'url': 'https://hoodline.com/2023/12/los-angeles-hit-with-no-burn-order-during-cloudy-holiday-forecast-aqmd-urges-compliance-for-public-health/', 'content': 'skies and a chance of rain. According to the National Weather Service, today’s weather in Los Angeles is mostly sunny  visiting the AQMD site or using its mobile app. While Los Angeles navigates through a cloudy and cooler weather  Weather & Environment in ... Los Angeles Hit with No-Burn Order During Cloudy Holiday Forecast  and cooler weather pattern, with temperatures fluctuating around the high 60 degrees and chances of rain by FridayPublished on December 26, 2023. Los Angeles residents face a restricted holiday season as the South Coast Air Quality Management District (AQMD) extends a mandatory no-burn order amid multiple ...'}, {'url': 'https://weather.com/weather/tenday/l/Los+Angeles+CA?canonicalCityId=84c64154109916077c8d3c2352410aaae5f6eeff682000e3a7470e38976128c2', 'content': 'recents Specialty Forecasts 10 Day Weather-Los Angeles, CA Tonight Sat 23 | Night  Considerable cloudiness with occasional rain showers. Low 48F. Winds light and variable. Chance of rain 60%. Thu 04  Mon 01 | Day Considerable clouds early. Some decrease in clouds later in the day. High 66F. Winds light and variable.  Thu 04 | Night Showers early becoming less numerous late. Low 48F. Winds light and variable. Chance of rain 40%. Fri 05Today 66°/ 50° 6% Sun 24 | Day 66° 6% WSW 4 mph Partly cloudy skies. High 66F. Winds light and variable. Humidity 65% UV Index 3 of 11 Sunrise 6:56 am Sunset 4:49 pm Sun 24 | Night 50° 10% N 1 mph...'}, {'url': 'https://weather.com/weather/tenday/l/Los+Angeles+CA?canonicalCityId=d4a04df2f5cd7d6ef329c49238253e994619763fd5f77a424ca3f1af9957e717', 'content': 'recents Specialty Forecasts 10 Day Weather-Los Angeles, CA Tonight Sat 23 | Night  Considerable cloudiness with occasional rain showers. Low 48F. Winds light and variable. Chance of rain 60%. Thu 04  Mon 01 | Day Considerable clouds early. Some decrease in clouds later in the day. High 66F. Winds light and variable.  Thu 04 | Night Showers early becoming less numerous late. Low 48F. Winds light and variable. Chance of rain 40%. Fri 0510 Day Weather - Los Angeles, CA As of 12:06 am PST Tonight --/ 49° 5% Sat 23 | Night 49° 5% NNW 2 mph Partly cloudy this evening, then becoming foggy and damp after midnight. Low 49F. Winds...'}]```\n",
      "------\n",
      "The weather in San Francisco is currently foggy early, then partly cloudy later in the day with a high around 60°F. There is a chance of rain showers with a high of 59°F tomorrow. The temperature will drop to a low of 46°F on Thursday night with cloudy skies and showers.\n",
      "\n",
      "In Los Angeles, there is considerable cloudiness with occasional rain showers. The temperature will drop to a low of 48°F tonight with light and variable winds. Tomorrow, there will be considerable clouds early with some decrease in clouds later in the day and a high of 66°F. Showers are expected in the evening with a low of 48°F.\n",
      "------\n"
     ]
    }
   ],
   "source": [
    "for chunk in agent_executor.stream({\"input\": \"what is the weather in SF and then LA\"}):\n",
    "    # Agent Action\n",
    "    if \"actions\" in chunk:\n",
    "        for action in chunk[\"actions\"]:\n",
    "            print(\n",
    "                f\"Calling Tool ```{action.tool}``` with input ```{action.tool_input}```\"\n",
    "            )\n",
    "    # Observation\n",
    "    elif \"steps\" in chunk:\n",
    "        for step in chunk[\"steps\"]:\n",
    "            print(f\"Got result: ```{step.observation}```\")\n",
    "    # Final result\n",
    "    elif \"output\" in chunk:\n",
    "        print(chunk[\"output\"])\n",
    "    else:\n",
    "        raise ValueError\n",
    "    print(\"------\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "72df7b43",
   "metadata": {},
   "source": [
    "### Using messages\n",
    "\n",
    "Using messages can be nice when working with chat applications - because everything is a message!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "ca79c8d9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\\n  \"query\": \"weather in San Francisco\"\\n}', 'name': 'tavily_search_results_json'}})]\n",
      "------\n",
      "[FunctionMessage(content='[{\"url\": \"https://www.cbsnews.com/sanfrancisco/news/next-round-of-rain-set-to-arrive-in-bay-area-wednesday-morning/\", \"content\": \"weather persists through Thursday morning. The second system is projected to get to the Bay Area early Friday,  Watch CBS News Next round of rain set to arrive in Bay Area Wednesday morning December 26, 2023 / 8:17 AM PST  to the Bay Area on Wednesday with the arrival of the first of two storm systems.  Overnight lows should be mostly in the 40s in the region, with some areas around the bay dropping into the 50s.Watch CBS News Weather Next round of rain set to arrive in Bay Area Wednesday morning December 26, 2023 / 8:17 AM PST / CBS/Bay City News Service While the outlook on Tuesday is cloudy and...\"}, {\"url\": \"https://weather.com/weather/tenday/l/San Francisco CA USCA0987:1:US\", \"content\": \"recents Specialty Forecasts 10 Day Weather-San Francisco, CA Today Mon 18 | Day  Fri 22 Fri 22 | Day Foggy early, then partly cloudy later in the day. High around 60F. Winds W at 10 to 15 mph.  Considerable cloudiness with occasional rain showers. High 59F. Winds SSE at 5 to 10 mph. Chance of rain 50%.  Thu 28 | Night Cloudy with showers. Low 46F. Winds S at 5 to 10 mph. Chance of rain 40%. Fri 29 Fri 29 | Day10 Day Weather-San Francisco, CA As of 5:52 pm PST alertLevel3 Coastal Flood Advisory+1 More Tonight Mostly Cloudy Night --/44° Rain 7% Arrow Up Sun 24| Night 44° Mostly Cloudy Night Rain 7%...\"}]', name='tavily_search_results_json')]\n",
      "------\n",
      "[AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\\n  \"query\": \"weather in Los Angeles\"\\n}', 'name': 'tavily_search_results_json'}})]\n",
      "------\n",
      "[FunctionMessage(content='[{\"url\": \"https://weather.com/weather/tenday/l/Los+Angeles+CA?canonicalCityId=d4a04df2f5cd7d6ef329c49238253e994619763fd5f77a424ca3f1af9957e717\", \"content\": \"recents Specialty Forecasts 10 Day Weather-Los Angeles, CA Tonight Sat 23 | Night  Considerable cloudiness with occasional rain showers. Low 48F. Winds light and variable. Chance of rain 60%. Thu 04  Mon 01 | Day Considerable clouds early. Some decrease in clouds later in the day. High 66F. Winds light and variable.  Thu 04 | Night Showers early becoming less numerous late. Low 48F. Winds light and variable. Chance of rain 40%. Fri 0510 Day Weather - Los Angeles, CA As of 12:06 am PST Tonight --/ 49° 5% Sat 23 | Night 49° 5% NNW 2 mph Partly cloudy this evening, then becoming foggy and damp after midnight. Low 49F. Winds...\"}, {\"url\": \"https://weather.com/weather/tenday/l/Los+Angeles+CA?canonicalCityId=84c64154109916077c8d3c2352410aaae5f6eeff682000e3a7470e38976128c2\", \"content\": \"recents Specialty Forecasts 10 Day Weather-Los Angeles, CA Tonight Sat 23 | Night  Considerable cloudiness with occasional rain showers. Low 48F. Winds light and variable. Chance of rain 60%. Thu 04  Mon 01 | Day Considerable clouds early. Some decrease in clouds later in the day. High 66F. Winds light and variable.  Thu 04 | Night Showers early becoming less numerous late. Low 48F. Winds light and variable. Chance of rain 40%. Fri 05Today 66°/ 50° 6% Sun 24 | Day 66° 6% WSW 4 mph Partly cloudy skies. High 66F. Winds light and variable. Humidity 65% UV Index 3 of 11 Sunrise 6:56 am Sunset 4:49 pm Sun 24 | Night 50° 10% N 1 mph...\"}, {\"url\": \"https://abc7.com/weather/\", \"content\": \"WATCH LIVE AccuWeather  in the region are forecast to be high.More  in the region are forecast to be high.More  NOW IN EFFECT FROM 4 AM THURSDAY TO 10 PM PST SATURDAY...MoreToday\\'s Weather Los Angeles, CA Current Today Tonight MOSTLY CLOUDY 65 ° Feels Like 65° Sunrise 6:55 AM Humidity 65% Sunset 4:48 PM Windspeed ESE 3 mph Moonrise 2:08 PM Pressure 30.0 in...\"}]', name='tavily_search_results_json')]\n",
      "------\n",
      "[AIMessage(content='The weather in San Francisco is expected to have rain on Wednesday and Thursday. The temperature will range from the 40s to the 50s. You can find more information [here](https://www.cbsnews.com/sanfrancisco/news/next-round-of-rain-set-to-arrive-in-bay-area-wednesday-morning/) and [here](https://weather.com/weather/tenday/l/San Francisco CA USCA0987:1:US).\\n\\nThe weather in Los Angeles is expected to have occasional rain showers with temperatures ranging from the 40s to the 60s. You can find more information [here](https://weather.com/weather/tenday/l/Los+Angeles+CA?canonicalCityId=d4a04df2f5cd7d6ef329c49238253e994619763fd5f77a424ca3f1af9957e717) and [here](https://weather.com/weather/tenday/l/Los+Angeles+CA?canonicalCityId=84c64154109916077c8d3c2352410aaae5f6eeff682000e3a7470e38976128c2).')]\n",
      "------\n"
     ]
    }
   ],
   "source": [
    "for chunk in agent_executor.stream({\"input\": \"what is the weather in SF and then LA\"}):\n",
    "    print(chunk[\"messages\"])\n",
    "    print(\"------\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0dc01b0f",
   "metadata": {},
   "source": [
    "## Stream tokens\n",
    "\n",
    "In addition to streaming the final result, you can also stream tokens. This will require slightly more complicated parsing of the logs\n",
    "\n",
    "You will also need to make sure you set the LLM to be streaming"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "3e92d09d",
   "metadata": {},
   "outputs": [],
   "source": [
    "llm = ChatOpenAI(model=\"gpt-3.5-turbo\", temperature=0, streaming=True)\n",
    "\n",
    "agent = create_openai_functions_agent(llm, tools, prompt)\n",
    "agent_executor = AgentExecutor(agent=agent, tools=tools)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "753ff598",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "RunLogPatch({'op': 'replace',\n",
      "  'path': '',\n",
      "  'value': {'final_output': None,\n",
      "            'id': '32650ba8-8a53-4b76-8846-dbb6c3a65727',\n",
      "            'logs': {},\n",
      "            'streamed_output': []}})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI',\n",
      "  'value': {'end_time': None,\n",
      "            'final_output': None,\n",
      "            'id': 'ce3a507d-210d-40aa-8576-dd0aa97e6498',\n",
      "            'metadata': {},\n",
      "            'name': 'ChatOpenAI',\n",
      "            'start_time': '2023-12-26T17:55:56.653',\n",
      "            'streamed_output': [],\n",
      "            'streamed_output_str': [],\n",
      "            'tags': ['seq:step:3'],\n",
      "            'type': 'llm'}})\n",
      "RunLogPatch({'op': 'add', 'path': '/logs/ChatOpenAI/streamed_output_str/-', 'value': ''},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='', additional_kwargs={'function_call': {'arguments': '', 'name': 'tavily_search_results_json'}})})\n",
      "RunLogPatch({'op': 'add', 'path': '/logs/ChatOpenAI/streamed_output_str/-', 'value': ''},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='', additional_kwargs={'function_call': {'arguments': '{\\n', 'name': ''}})})\n",
      "RunLogPatch({'op': 'add', 'path': '/logs/ChatOpenAI/streamed_output_str/-', 'value': ''},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='', additional_kwargs={'function_call': {'arguments': ' ', 'name': ''}})})\n",
      "RunLogPatch({'op': 'add', 'path': '/logs/ChatOpenAI/streamed_output_str/-', 'value': ''},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='', additional_kwargs={'function_call': {'arguments': ' \"', 'name': ''}})})\n",
      "RunLogPatch({'op': 'add', 'path': '/logs/ChatOpenAI/streamed_output_str/-', 'value': ''},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='', additional_kwargs={'function_call': {'arguments': 'query', 'name': ''}})})\n",
      "RunLogPatch({'op': 'add', 'path': '/logs/ChatOpenAI/streamed_output_str/-', 'value': ''},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='', additional_kwargs={'function_call': {'arguments': '\":', 'name': ''}})})\n",
      "RunLogPatch({'op': 'add', 'path': '/logs/ChatOpenAI/streamed_output_str/-', 'value': ''},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='', additional_kwargs={'function_call': {'arguments': ' \"', 'name': ''}})})\n",
      "RunLogPatch({'op': 'add', 'path': '/logs/ChatOpenAI/streamed_output_str/-', 'value': ''},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='', additional_kwargs={'function_call': {'arguments': 'weather', 'name': ''}})})\n",
      "RunLogPatch({'op': 'add', 'path': '/logs/ChatOpenAI/streamed_output_str/-', 'value': ''},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='', additional_kwargs={'function_call': {'arguments': ' in', 'name': ''}})})\n",
      "RunLogPatch({'op': 'add', 'path': '/logs/ChatOpenAI/streamed_output_str/-', 'value': ''},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='', additional_kwargs={'function_call': {'arguments': ' San', 'name': ''}})})\n",
      "RunLogPatch({'op': 'add', 'path': '/logs/ChatOpenAI/streamed_output_str/-', 'value': ''},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='', additional_kwargs={'function_call': {'arguments': ' Francisco', 'name': ''}})})\n",
      "RunLogPatch({'op': 'add', 'path': '/logs/ChatOpenAI/streamed_output_str/-', 'value': ''},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='', additional_kwargs={'function_call': {'arguments': '\"\\n', 'name': ''}})})\n",
      "RunLogPatch({'op': 'add', 'path': '/logs/ChatOpenAI/streamed_output_str/-', 'value': ''},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='', additional_kwargs={'function_call': {'arguments': '}', 'name': ''}})})\n",
      "RunLogPatch({'op': 'add', 'path': '/logs/ChatOpenAI/streamed_output_str/-', 'value': ''},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI/final_output',\n",
      "  'value': {'generations': [[{'generation_info': {'finish_reason': 'function_call'},\n",
      "                              'message': AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\\n  \"query\": \"weather in San Francisco\"\\n}', 'name': 'tavily_search_results_json'}}),\n",
      "                              'text': '',\n",
      "                              'type': 'ChatGeneration'}]],\n",
      "            'llm_output': None,\n",
      "            'run': None}},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI/end_time',\n",
      "  'value': '2023-12-26T17:55:57.337'})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/streamed_output/-',\n",
      "  'value': {'actions': [AgentActionMessageLog(tool='tavily_search_results_json', tool_input={'query': 'weather in San Francisco'}, log=\"\\nInvoking: `tavily_search_results_json` with `{'query': 'weather in San Francisco'}`\\n\\n\\n\", message_log=[AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\\n  \"query\": \"weather in San Francisco\"\\n}', 'name': 'tavily_search_results_json'}})])],\n",
      "            'messages': [AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\\n  \"query\": \"weather in San Francisco\"\\n}', 'name': 'tavily_search_results_json'}})]}},\n",
      " {'op': 'replace',\n",
      "  'path': '/final_output',\n",
      "  'value': {'actions': [AgentActionMessageLog(tool='tavily_search_results_json', tool_input={'query': 'weather in San Francisco'}, log=\"\\nInvoking: `tavily_search_results_json` with `{'query': 'weather in San Francisco'}`\\n\\n\\n\", message_log=[AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\\n  \"query\": \"weather in San Francisco\"\\n}', 'name': 'tavily_search_results_json'}})])],\n",
      "            'messages': [AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\\n  \"query\": \"weather in San Francisco\"\\n}', 'name': 'tavily_search_results_json'}})]}})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/streamed_output/-',\n",
      "  'value': {'messages': [FunctionMessage(content='[{\"url\": \"https://www.cbsnews.com/sanfrancisco/news/next-round-of-rain-set-to-arrive-in-bay-area-wednesday-morning/\", \"content\": \"weather persists through Thursday morning. The second system is projected to get to the Bay Area early Friday,  Watch CBS News Next round of rain set to arrive in Bay Area Wednesday morning December 26, 2023 / 8:17 AM PST  to the Bay Area on Wednesday with the arrival of the first of two storm systems.  Overnight lows should be mostly in the 40s in the region, with some areas around the bay dropping into the 50s.Watch CBS News Weather Next round of rain set to arrive in Bay Area Wednesday morning December 26, 2023 / 8:17 AM PST / CBS/Bay City News Service While the outlook on Tuesday is cloudy and...\"}, {\"url\": \"https://weather.com/weather/tenday/l/San Francisco CA USCA0987:1:US\", \"content\": \"recents Specialty Forecasts 10 Day Weather-San Francisco, CA Today Mon 18 | Day  Fri 22 Fri 22 | Day Foggy early, then partly cloudy later in the day. High around 60F. Winds W at 10 to 15 mph.  Considerable cloudiness with occasional rain showers. High 59F. Winds SSE at 5 to 10 mph. Chance of rain 50%.  Thu 28 | Night Cloudy with showers. Low 46F. Winds S at 5 to 10 mph. Chance of rain 40%. Fri 29 Fri 29 | Day10 Day Weather-San Francisco, CA As of 5:52 pm PST alertLevel3 Coastal Flood Advisory+1 More Tonight Mostly Cloudy Night --/44° Rain 7% Arrow Up Sun 24| Night 44° Mostly Cloudy Night Rain 7%...\"}]', name='tavily_search_results_json')],\n",
      "            'steps': [AgentStep(action=AgentActionMessageLog(tool='tavily_search_results_json', tool_input={'query': 'weather in San Francisco'}, log=\"\\nInvoking: `tavily_search_results_json` with `{'query': 'weather in San Francisco'}`\\n\\n\\n\", message_log=[AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\\n  \"query\": \"weather in San Francisco\"\\n}', 'name': 'tavily_search_results_json'}})]), observation=[{'url': 'https://www.cbsnews.com/sanfrancisco/news/next-round-of-rain-set-to-arrive-in-bay-area-wednesday-morning/', 'content': 'weather persists through Thursday morning. The second system is projected to get to the Bay Area early Friday,  Watch CBS News Next round of rain set to arrive in Bay Area Wednesday morning December 26, 2023 / 8:17 AM PST  to the Bay Area on Wednesday with the arrival of the first of two storm systems.  Overnight lows should be mostly in the 40s in the region, with some areas around the bay dropping into the 50s.Watch CBS News Weather Next round of rain set to arrive in Bay Area Wednesday morning December 26, 2023 / 8:17 AM PST / CBS/Bay City News Service While the outlook on Tuesday is cloudy and...'}, {'url': 'https://weather.com/weather/tenday/l/San Francisco CA USCA0987:1:US', 'content': 'recents Specialty Forecasts 10 Day Weather-San Francisco, CA Today Mon 18 | Day  Fri 22 Fri 22 | Day Foggy early, then partly cloudy later in the day. High around 60F. Winds W at 10 to 15 mph.  Considerable cloudiness with occasional rain showers. High 59F. Winds SSE at 5 to 10 mph. Chance of rain 50%.  Thu 28 | Night Cloudy with showers. Low 46F. Winds S at 5 to 10 mph. Chance of rain 40%. Fri 29 Fri 29 | Day10 Day Weather-San Francisco, CA As of 5:52 pm PST alertLevel3 Coastal Flood Advisory+1 More Tonight Mostly Cloudy Night --/44° Rain 7% Arrow Up Sun 24| Night 44° Mostly Cloudy Night Rain 7%...'}])]}},\n",
      " {'op': 'add',\n",
      "  'path': '/final_output/steps',\n",
      "  'value': [AgentStep(action=AgentActionMessageLog(tool='tavily_search_results_json', tool_input={'query': 'weather in San Francisco'}, log=\"\\nInvoking: `tavily_search_results_json` with `{'query': 'weather in San Francisco'}`\\n\\n\\n\", message_log=[AIMessage(content='', additional_kwargs={'function_call': {'arguments': '{\\n  \"query\": \"weather in San Francisco\"\\n}', 'name': 'tavily_search_results_json'}})]), observation=[{'url': 'https://www.cbsnews.com/sanfrancisco/news/next-round-of-rain-set-to-arrive-in-bay-area-wednesday-morning/', 'content': 'weather persists through Thursday morning. The second system is projected to get to the Bay Area early Friday,  Watch CBS News Next round of rain set to arrive in Bay Area Wednesday morning December 26, 2023 / 8:17 AM PST  to the Bay Area on Wednesday with the arrival of the first of two storm systems.  Overnight lows should be mostly in the 40s in the region, with some areas around the bay dropping into the 50s.Watch CBS News Weather Next round of rain set to arrive in Bay Area Wednesday morning December 26, 2023 / 8:17 AM PST / CBS/Bay City News Service While the outlook on Tuesday is cloudy and...'}, {'url': 'https://weather.com/weather/tenday/l/San Francisco CA USCA0987:1:US', 'content': 'recents Specialty Forecasts 10 Day Weather-San Francisco, CA Today Mon 18 | Day  Fri 22 Fri 22 | Day Foggy early, then partly cloudy later in the day. High around 60F. Winds W at 10 to 15 mph.  Considerable cloudiness with occasional rain showers. High 59F. Winds SSE at 5 to 10 mph. Chance of rain 50%.  Thu 28 | Night Cloudy with showers. Low 46F. Winds S at 5 to 10 mph. Chance of rain 40%. Fri 29 Fri 29 | Day10 Day Weather-San Francisco, CA As of 5:52 pm PST alertLevel3 Coastal Flood Advisory+1 More Tonight Mostly Cloudy Night --/44° Rain 7% Arrow Up Sun 24| Night 44° Mostly Cloudy Night Rain 7%...'}])]},\n",
      " {'op': 'add',\n",
      "  'path': '/final_output/messages/1',\n",
      "  'value': FunctionMessage(content='[{\"url\": \"https://www.cbsnews.com/sanfrancisco/news/next-round-of-rain-set-to-arrive-in-bay-area-wednesday-morning/\", \"content\": \"weather persists through Thursday morning. The second system is projected to get to the Bay Area early Friday,  Watch CBS News Next round of rain set to arrive in Bay Area Wednesday morning December 26, 2023 / 8:17 AM PST  to the Bay Area on Wednesday with the arrival of the first of two storm systems.  Overnight lows should be mostly in the 40s in the region, with some areas around the bay dropping into the 50s.Watch CBS News Weather Next round of rain set to arrive in Bay Area Wednesday morning December 26, 2023 / 8:17 AM PST / CBS/Bay City News Service While the outlook on Tuesday is cloudy and...\"}, {\"url\": \"https://weather.com/weather/tenday/l/San Francisco CA USCA0987:1:US\", \"content\": \"recents Specialty Forecasts 10 Day Weather-San Francisco, CA Today Mon 18 | Day  Fri 22 Fri 22 | Day Foggy early, then partly cloudy later in the day. High around 60F. Winds W at 10 to 15 mph.  Considerable cloudiness with occasional rain showers. High 59F. Winds SSE at 5 to 10 mph. Chance of rain 50%.  Thu 28 | Night Cloudy with showers. Low 46F. Winds S at 5 to 10 mph. Chance of rain 40%. Fri 29 Fri 29 | Day10 Day Weather-San Francisco, CA As of 5:52 pm PST alertLevel3 Coastal Flood Advisory+1 More Tonight Mostly Cloudy Night --/44° Rain 7% Arrow Up Sun 24| Night 44° Mostly Cloudy Night Rain 7%...\"}]', name='tavily_search_results_json')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2',\n",
      "  'value': {'end_time': None,\n",
      "            'final_output': None,\n",
      "            'id': '503b959e-b6c2-427b-b2e8-7d40497a2458',\n",
      "            'metadata': {},\n",
      "            'name': 'ChatOpenAI',\n",
      "            'start_time': '2023-12-26T17:56:00.983',\n",
      "            'streamed_output': [],\n",
      "            'streamed_output_str': [],\n",
      "            'tags': ['seq:step:3'],\n",
      "            'type': 'llm'}})\n",
      "RunLogPatch({'op': 'add', 'path': '/logs/ChatOpenAI:2/streamed_output_str/-', 'value': ''},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': 'The'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='The')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' weather'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' weather')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' in'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' in')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' San'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' San')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' Francisco'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' Francisco')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' is'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' is')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' currently'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' currently')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' cloudy'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' cloudy')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' with'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' with')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' occasional'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' occasional')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' rain'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' rain')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' showers'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' showers')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': '.'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='.')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' The'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' The')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' temperature'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' temperature')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' is'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' is')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' around'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' around')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' '},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' ')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': '59'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='59')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': '°F'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='°F')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' ('},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' (')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': '15'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='15')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': '°C'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='°C')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ')'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=')')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' with'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' with')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' winds'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' winds')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' from'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' from')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' the'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' the')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' southeast'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' southeast')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' at'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' at')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' '},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' ')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': '5'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='5')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' to'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' to')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' '},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' ')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': '10'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='10')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' mph'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' mph')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': '.'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='.')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' The'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' The')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' overnight'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' overnight')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' low'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' low')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' is'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' is')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' expected'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' expected')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' to'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' to')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' be'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' be')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' around'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' around')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' '},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' ')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': '46'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='46')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': '°F'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='°F')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' ('},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' (')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': '8'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='8')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': '°C'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='°C')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ')'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=')')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' with'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' with')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' a'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' a')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' chance'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' chance')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' of'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' of')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': ' showers'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content=' showers')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output_str/-',\n",
      "  'value': '.'},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='.')})\n",
      "RunLogPatch({'op': 'add', 'path': '/logs/ChatOpenAI:2/streamed_output_str/-', 'value': ''},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/streamed_output/-',\n",
      "  'value': AIMessageChunk(content='')})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/final_output',\n",
      "  'value': {'generations': [[{'generation_info': {'finish_reason': 'stop'},\n",
      "                              'message': AIMessage(content='The weather in San Francisco is currently cloudy with occasional rain showers. The temperature is around 59°F (15°C) with winds from the southeast at 5 to 10 mph. The overnight low is expected to be around 46°F (8°C) with a chance of showers.'),\n",
      "                              'text': 'The weather in San Francisco is '\n",
      "                                      'currently cloudy with occasional rain '\n",
      "                                      'showers. The temperature is around 59°F '\n",
      "                                      '(15°C) with winds from the southeast at '\n",
      "                                      '5 to 10 mph. The overnight low is '\n",
      "                                      'expected to be around 46°F (8°C) with a '\n",
      "                                      'chance of showers.',\n",
      "                              'type': 'ChatGeneration'}]],\n",
      "            'llm_output': None,\n",
      "            'run': None}},\n",
      " {'op': 'add',\n",
      "  'path': '/logs/ChatOpenAI:2/end_time',\n",
      "  'value': '2023-12-26T17:56:02.356'})\n",
      "RunLogPatch({'op': 'add',\n",
      "  'path': '/streamed_output/-',\n",
      "  'value': {'messages': [AIMessage(content='The weather in San Francisco is currently cloudy with occasional rain showers. The temperature is around 59°F (15°C) with winds from the southeast at 5 to 10 mph. The overnight low is expected to be around 46°F (8°C) with a chance of showers.')],\n",
      "            'output': 'The weather in San Francisco is currently cloudy with '\n",
      "                      'occasional rain showers. The temperature is around 59°F '\n",
      "                      '(15°C) with winds from the southeast at 5 to 10 mph. '\n",
      "                      'The overnight low is expected to be around 46°F (8°C) '\n",
      "                      'with a chance of showers.'}},\n",
      " {'op': 'add',\n",
      "  'path': '/final_output/output',\n",
      "  'value': 'The weather in San Francisco is currently cloudy with occasional '\n",
      "           'rain showers. The temperature is around 59°F (15°C) with winds '\n",
      "           'from the southeast at 5 to 10 mph. The overnight low is expected '\n",
      "           'to be around 46°F (8°C) with a chance of showers.'},\n",
      " {'op': 'add',\n",
      "  'path': '/final_output/messages/2',\n",
      "  'value': AIMessage(content='The weather in San Francisco is currently cloudy with occasional rain showers. The temperature is around 59°F (15°C) with winds from the southeast at 5 to 10 mph. The overnight low is expected to be around 46°F (8°C) with a chance of showers.')})\n"
     ]
    }
   ],
   "source": [
    "async for chunk in agent_executor.astream_log(\n",
    "    {\"input\": \"what is the weather in sf\", \"chat_history\": []},\n",
    "    include_names=[\"ChatOpenAI\"],\n",
    "):\n",
    "    print(chunk)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "51a51076",
   "metadata": {},
   "source": [
    "This may require some logic to get in a workable format"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "7cdae318",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "None\n",
      "----\n",
      "/logs/ChatOpenAI\n",
      "{'id': '3f6d3587-600f-419b-8225-8908a347b7d2', 'name': 'ChatOpenAI', 'type': 'llm', 'tags': ['seq:step:3'], 'metadata': {}, 'start_time': '2023-12-26T17:56:19.884', 'streamed_output': [], 'streamed_output_str': [], 'final_output': None, 'end_time': None}\n",
      "----\n",
      "/logs/ChatOpenAI/streamed_output/-\n",
      "content='' additional_kwargs={'function_call': {'arguments': '', 'name': 'tavily_search_results_json'}}\n",
      "----\n",
      "/logs/ChatOpenAI/streamed_output/-\n",
      "content='' additional_kwargs={'function_call': {'arguments': '{\\n', 'name': 'tavily_search_results_json'}}\n",
      "----\n",
      "/logs/ChatOpenAI/streamed_output/-\n",
      "content='' additional_kwargs={'function_call': {'arguments': '{\\n ', 'name': 'tavily_search_results_json'}}\n",
      "----\n",
      "/logs/ChatOpenAI/streamed_output/-\n",
      "content='' additional_kwargs={'function_call': {'arguments': '{\\n  \"', 'name': 'tavily_search_results_json'}}\n",
      "----\n",
      "/logs/ChatOpenAI/streamed_output/-\n",
      "content='' additional_kwargs={'function_call': {'arguments': '{\\n  \"query', 'name': 'tavily_search_results_json'}}\n",
      "----\n",
      "/logs/ChatOpenAI/streamed_output/-\n",
      "content='' additional_kwargs={'function_call': {'arguments': '{\\n  \"query\":', 'name': 'tavily_search_results_json'}}\n",
      "----\n",
      "/logs/ChatOpenAI/streamed_output/-\n",
      "content='' additional_kwargs={'function_call': {'arguments': '{\\n  \"query\": \"', 'name': 'tavily_search_results_json'}}\n",
      "----\n",
      "/logs/ChatOpenAI/streamed_output/-\n",
      "content='' additional_kwargs={'function_call': {'arguments': '{\\n  \"query\": \"weather', 'name': 'tavily_search_results_json'}}\n",
      "----\n",
      "/logs/ChatOpenAI/streamed_output/-\n",
      "content='' additional_kwargs={'function_call': {'arguments': '{\\n  \"query\": \"weather in', 'name': 'tavily_search_results_json'}}\n",
      "----\n",
      "/logs/ChatOpenAI/streamed_output/-\n",
      "content='' additional_kwargs={'function_call': {'arguments': '{\\n  \"query\": \"weather in San', 'name': 'tavily_search_results_json'}}\n",
      "----\n",
      "/logs/ChatOpenAI/streamed_output/-\n",
      "content='' additional_kwargs={'function_call': {'arguments': '{\\n  \"query\": \"weather in San Francisco', 'name': 'tavily_search_results_json'}}\n",
      "----\n",
      "/logs/ChatOpenAI/streamed_output/-\n",
      "content='' additional_kwargs={'function_call': {'arguments': '{\\n  \"query\": \"weather in San Francisco\"\\n', 'name': 'tavily_search_results_json'}}\n",
      "----\n",
      "/logs/ChatOpenAI/streamed_output/-\n",
      "content='' additional_kwargs={'function_call': {'arguments': '{\\n  \"query\": \"weather in San Francisco\"\\n}', 'name': 'tavily_search_results_json'}}\n",
      "----\n",
      "/logs/ChatOpenAI/streamed_output/-\n",
      "content='' additional_kwargs={'function_call': {'arguments': '{\\n  \"query\": \"weather in San Francisco\"\\n}', 'name': 'tavily_search_results_json'}}\n",
      "----\n",
      "/logs/ChatOpenAI/end_time\n",
      "2023-12-26T17:56:20.849\n",
      "----\n",
      "/final_output\n",
      "None\n",
      "----\n",
      "/final_output/messages/1\n",
      "content='[{\"url\": \"https://weather.com/weather/tenday/l/San Francisco CA USCA0987:1:US\", \"content\": \"recents Specialty Forecasts 10 Day Weather-San Francisco, CA Today Mon 18 | Day  Fri 22 Fri 22 | Day Foggy early, then partly cloudy later in the day. High around 60F. Winds W at 10 to 15 mph.  Considerable cloudiness with occasional rain showers. High 59F. Winds SSE at 5 to 10 mph. Chance of rain 50%.  Thu 28 | Night Cloudy with showers. Low 46F. Winds S at 5 to 10 mph. Chance of rain 40%. Fri 29 Fri 29 | Day10 Day Weather-San Francisco, CA As of 5:52 pm PST alertLevel3 Coastal Flood Advisory+1 More Tonight Mostly Cloudy Night --/44° Rain 7% Arrow Up Sun 24| Night 44° Mostly Cloudy Night Rain 7%...\"}]' name='tavily_search_results_json'\n",
      "----\n",
      "/logs/ChatOpenAI:2\n",
      "{'id': 'fc7ab413-6f59-4a9e-bae1-3140abdaff55', 'name': 'ChatOpenAI', 'type': 'llm', 'tags': ['seq:step:3'], 'metadata': {}, 'start_time': '2023-12-26T17:56:24.546', 'streamed_output': [], 'streamed_output_str': [], 'final_output': None, 'end_time': None}\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content=''\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently fog'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a high'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a high around'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a high around '\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a high around 60'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a high around 60°F'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a high around 60°F.'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a high around 60°F. Later'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a high around 60°F. Later in'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a high around 60°F. Later in the'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a high around 60°F. Later in the day'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a high around 60°F. Later in the day,'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a high around 60°F. Later in the day, it'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a high around 60°F. Later in the day, it will'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a high around 60°F. Later in the day, it will become'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a high around 60°F. Later in the day, it will become partly'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a high around 60°F. Later in the day, it will become partly cloudy'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a high around 60°F. Later in the day, it will become partly cloudy.'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a high around 60°F. Later in the day, it will become partly cloudy. The'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a high around 60°F. Later in the day, it will become partly cloudy. The wind'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a high around 60°F. Later in the day, it will become partly cloudy. The wind is'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a high around 60°F. Later in the day, it will become partly cloudy. The wind is coming'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a high around 60°F. Later in the day, it will become partly cloudy. The wind is coming from'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a high around 60°F. Later in the day, it will become partly cloudy. The wind is coming from the'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a high around 60°F. Later in the day, it will become partly cloudy. The wind is coming from the west'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a high around 60°F. Later in the day, it will become partly cloudy. The wind is coming from the west at'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a high around 60°F. Later in the day, it will become partly cloudy. The wind is coming from the west at '\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a high around 60°F. Later in the day, it will become partly cloudy. The wind is coming from the west at 10'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a high around 60°F. Later in the day, it will become partly cloudy. The wind is coming from the west at 10 to'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a high around 60°F. Later in the day, it will become partly cloudy. The wind is coming from the west at 10 to '\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a high around 60°F. Later in the day, it will become partly cloudy. The wind is coming from the west at 10 to 15'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a high around 60°F. Later in the day, it will become partly cloudy. The wind is coming from the west at 10 to 15 mph'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a high around 60°F. Later in the day, it will become partly cloudy. The wind is coming from the west at 10 to 15 mph.'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a high around 60°F. Later in the day, it will become partly cloudy. The wind is coming from the west at 10 to 15 mph. Tomorrow'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a high around 60°F. Later in the day, it will become partly cloudy. The wind is coming from the west at 10 to 15 mph. Tomorrow,'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a high around 60°F. Later in the day, it will become partly cloudy. The wind is coming from the west at 10 to 15 mph. Tomorrow, there'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a high around 60°F. Later in the day, it will become partly cloudy. The wind is coming from the west at 10 to 15 mph. Tomorrow, there will'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a high around 60°F. Later in the day, it will become partly cloudy. The wind is coming from the west at 10 to 15 mph. Tomorrow, there will be'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a high around 60°F. Later in the day, it will become partly cloudy. The wind is coming from the west at 10 to 15 mph. Tomorrow, there will be considerable'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a high around 60°F. Later in the day, it will become partly cloudy. The wind is coming from the west at 10 to 15 mph. Tomorrow, there will be considerable cloud'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a high around 60°F. Later in the day, it will become partly cloudy. The wind is coming from the west at 10 to 15 mph. Tomorrow, there will be considerable cloudiness'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a high around 60°F. Later in the day, it will become partly cloudy. The wind is coming from the west at 10 to 15 mph. Tomorrow, there will be considerable cloudiness with'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a high around 60°F. Later in the day, it will become partly cloudy. The wind is coming from the west at 10 to 15 mph. Tomorrow, there will be considerable cloudiness with occasional'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a high around 60°F. Later in the day, it will become partly cloudy. The wind is coming from the west at 10 to 15 mph. Tomorrow, there will be considerable cloudiness with occasional rain'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a high around 60°F. Later in the day, it will become partly cloudy. The wind is coming from the west at 10 to 15 mph. Tomorrow, there will be considerable cloudiness with occasional rain showers'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a high around 60°F. Later in the day, it will become partly cloudy. The wind is coming from the west at 10 to 15 mph. Tomorrow, there will be considerable cloudiness with occasional rain showers and'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a high around 60°F. Later in the day, it will become partly cloudy. The wind is coming from the west at 10 to 15 mph. Tomorrow, there will be considerable cloudiness with occasional rain showers and a'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a high around 60°F. Later in the day, it will become partly cloudy. The wind is coming from the west at 10 to 15 mph. Tomorrow, there will be considerable cloudiness with occasional rain showers and a high'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a high around 60°F. Later in the day, it will become partly cloudy. The wind is coming from the west at 10 to 15 mph. Tomorrow, there will be considerable cloudiness with occasional rain showers and a high of'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a high around 60°F. Later in the day, it will become partly cloudy. The wind is coming from the west at 10 to 15 mph. Tomorrow, there will be considerable cloudiness with occasional rain showers and a high of '\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a high around 60°F. Later in the day, it will become partly cloudy. The wind is coming from the west at 10 to 15 mph. Tomorrow, there will be considerable cloudiness with occasional rain showers and a high of 59'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a high around 60°F. Later in the day, it will become partly cloudy. The wind is coming from the west at 10 to 15 mph. Tomorrow, there will be considerable cloudiness with occasional rain showers and a high of 59°F'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a high around 60°F. Later in the day, it will become partly cloudy. The wind is coming from the west at 10 to 15 mph. Tomorrow, there will be considerable cloudiness with occasional rain showers and a high of 59°F.'\n",
      "----\n",
      "/logs/ChatOpenAI:2/streamed_output/-\n",
      "content='The weather in San Francisco is currently foggy with a high around 60°F. Later in the day, it will become partly cloudy. The wind is coming from the west at 10 to 15 mph. Tomorrow, there will be considerable cloudiness with occasional rain showers and a high of 59°F.'\n",
      "----\n",
      "/logs/ChatOpenAI:2/end_time\n",
      "2023-12-26T17:56:25.673\n",
      "----\n",
      "/final_output/messages/2\n",
      "content='The weather in San Francisco is currently foggy with a high around 60°F. Later in the day, it will become partly cloudy. The wind is coming from the west at 10 to 15 mph. Tomorrow, there will be considerable cloudiness with occasional rain showers and a high of 59°F.'\n",
      "----\n"
     ]
    }
   ],
   "source": [
    "path_status = {}\n",
    "async for chunk in agent_executor.astream_log(\n",
    "    {\"input\": \"what is the weather in sf\", \"chat_history\": []},\n",
    "    include_names=[\"ChatOpenAI\"],\n",
    "):\n",
    "    for op in chunk.ops:\n",
    "        if op[\"op\"] == \"add\":\n",
    "            if op[\"path\"] not in path_status:\n",
    "                path_status[op[\"path\"]] = op[\"value\"]\n",
    "            else:\n",
    "                path_status[op[\"path\"]] += op[\"value\"]\n",
    "    print(op[\"path\"])\n",
    "    print(path_status.get(op[\"path\"]))\n",
    "    print(\"----\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4fdfc76d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
